
layui.use(['jquery','layer','form','table','laydate','upload'],function(){
    var $ = layui.$,layer = layui.layer,form = layui.form,table = layui.table,laydate = layui.laydate,upload = layui.upload;

    //普同检索
    form.on('submit(sub)',function(data){
        $.ajax({
            url: '/user/getUsers'
            ,data: {
                'account': data.field.account
                ,'nickname': data.field.nickname
                ,'createDate': data.field.createDate
            }
            ,dataType: 'json'
            ,success:function(data){
                tableRender(data.data);
            }
        });
    });




//获取所有前端用户
getUsers();
function getUsers(userId){
    $.ajax({
        url: '/user/getUsers'
        ,data: {'userId': userId}
        ,dataType: 'json'
        ,success:function(data){
            //渲染表格
            tableRender(data.data);
        }
    });
}
//渲染表格
function tableRender(data){
    table.render({
        elem: '#layuiTbale'
        ,data: data
        ,toolbar: '#toolutil'
        ,page: {
            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']//自定义分页布局
            ,limits:[10,15,20]
            ,first: true
            ,last: true
        }
        ,limit: 10
        ,cols: [[
            {checkbox: true}
            ,{type: 'numbers',title: '序号',align: 'center'}
            ,{field: 'userId',title: '用户编号',align: 'center'}
            ,{field: 'account',title: '用户账号',align: 'center'}
            ,{field: 'nickname',title: '用户昵称',align: 'center'}
            ,{field: 'studyDate',title: '学习时常',align: 'center'}
            ,{field: 'consumption',title: '用户消费总金额',align: 'center'}
            ,{field: 'createDate',title: '账户注册时间',align: 'center'}
            ,{field: 'status',title: '状态',align: 'center',templet:function(data){
                    if(data.status == 2){
                        return '<input type="checkbox" name="status" value="'+data.status+'" disabled="disabled" lay-filter="switch" lay-skin="switch" lay-text="正常|停用" checked>'
                    }else if(data.status == 5){
                        return '<input type="checkbox" name="status" value="'+data.status+'" disabled="disabled" lay-filter="switch" lay-skin="switch" lay-text="正常|停用">'
                    }else{
                        return '数据错误';
                    }
                }}
            ,{title: '操作',align: 'center',templet:function(data){
                    return '<a onclick="see('+data.userId+')" style="color: #009688;">查看</a> ' +
                        '<a onclick="edit('+data.userId+')" style="color: #009688;">编辑</a> ' +
                        '<a onclick="shutDown(\''+data.userId+'\',\''+data.account+'\',\''+data.status+'\')" style="color: #009688;">封停</a>'
                }}

        ]]
    });
}

//高级检索
advancedRetrieval = function advancedRetrieval(){
    layer.open({
        type: 2
        ,title: '高级检索'
        ,content: 'popup/rotationChart-list-advancedRetrieval.html'
        ,area: ['650px','330px']
    });
}


//查看
see = function see(id){
    $('#userId').val(id);
    layer.open({
        type: 2
        ,title: '用户详情信息'
        ,content: 'popup/rotationChart-list-see.html'
        ,area: ['960px','610px']
    });
}

//编辑
edit = function edit(id){
    $('#userId').val(id);
    layer.open({
        type: 2
        ,title: '编辑用户'
        ,content: 'popup/rotationChart-list-edit.html'
        ,area: ['960px','610px']
    });
}

//封停
shutDown = function shutDown(id,account,status){
    $('#userId').val(id);
    $('#account').val(account);
    if(status == 5){
        layer.msg('该账户已被封禁');
    }else{
        layer.open({
            type: 2
            ,title: '封停用户'
            ,content: 'popup/rotationChart-list-shutDown.html'
            ,area: ['500px','500px']
        });
    }
}
//存储UserId
    var users = [];
//添加进思用户
table.on('checkbox(layuiTbale)', function(obj){
    //查询元素下标
    Array.prototype.indexOf = function (val) {
        for (var i=0; i<this.length; i++){
            if(this[i] == val)
                return i;
        }
        return -1;
    }
    //删除元素
    Array.prototype.remove = function(val) {
        var index = this.indexOf(val);
        if (index > -1) {
            this.splice(index, 1);
        }
    };
    if(obj.checked){
        users.push(obj.data.userId);
    }else{
        users.remove(obj.data.userId);
    }
});

//添加进思用户 22/1/13
addJinsiUsers = function addJinsiUsers(){
    if(users.length == 1){
        $.ajax({
            url: '/user/determineUser'
            ,data: {'userId': users[0]}
            ,dataType: 'json'
            ,success:function(data){
                if(data.data == 1){
                    layer.msg('此用户已是进思用户');
                }else if(data.data == 2){
                    $('#userId').val(users[0]);
                    layer.open({
                        type: 2
                        ,content: 'popup/rotationChart-list-add-jinsi-users.html'
                        ,title: '添加进思用户'
                        ,area: ['600px','800px']
                    });
                }else{
                    layer.msg('后台数据错误');
                }
            }
        });
    }else if(users.length > 1){
        layer.msg('由于需要录入详情信息，所以只能选择一名用户');
    }else if(users.length == 0){
        layer.msg('请至少选择一名用户');
    }else{
        layer.msg('数据错误');
    }
}

//赠送优惠券
giveCoupon = function giveCoupon(){
    for (var i=0; i<users.length; i++){
        $('#userId').val($('#userId').val()+users[i]);
    }
    if(users.length >= 1){
        layer.open({
            type: 2
            ,content: 'popup/rotationChart-list-coupon-list.html'
            ,title: '赠送优惠券'
            ,area: ['1000px','650px']
        });
    }else{
        layer.msg('请至少选择一名用户');
    }

}


//刷新
refresh = function refresh(){
    location.reload();

}

//返回



});
//返回首页
function backOff(){
    parent.jumpSystemHome();
}